﻿using System.Collections.Generic;
using System.Linq;

namespace Data
{
    public static class SiteUserRepository
    {
        private static readonly DiaryEntities Db = new DiaryEntities();

        public static bool ValidateUser(string username, string password)
        {
            var thisuser = (from user in Db.users
                            where user.username == username
                                    && user.password == password
                                    && user.deleted == null
                            select user).ToList();
            return (thisuser.Count() != 0);
        }

        public static SiteUserModels.SiteUserModel LoadLoggedInUserDetails(string username)
        {
            var thisuser = (from user in Db.users
                            where user.username == username
                            && user.deleted == null
                            select user).FirstOrDefault();

            SiteUserModels.SiteUserModel usermodel = new SiteUserModels.SiteUserModel
            {
                firstname = thisuser.firstname,
                user_id = thisuser.user_id

            };

            return usermodel;

        }

        public static string CreateUser(string username, string email, string password, string firstname, string surname, int timezoneId)
        {
            user u = new user();
            u.surname = surname;
            u.firstname = firstname;
            u.email = email;
            u.z_timezone = Db.z_timezone.FirstOrDefault(tz => tz.timezone_id == timezoneId);
            u.password = password;
            u.username = username;
            Db.AddTousers(u);

            Db.SaveChanges();

            return username;
            
        }

        public static List<z_timezone> GetTimezones()
        {
            var list = (from z_timezone in Db.z_timezone
                    select z_timezone).ToList();
            return list;
        }
    }
}